using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProgrammingEducationSystem.Models;
using ProgrammingEducationSystem.Repository.IRepository;
using ProgrammingEducationSystem.Services.IServices;
using ProgrammingEducationSystem.Tools;

namespace ProgrammingEducationSystem.Services
{
    public class CourseHoursService : ICourseHoursService
    {
        private readonly ILearnSourceRepository _learnSourceRepository;
        private readonly IClassRepository _classRepository;
        private readonly ICourseHourRepository _courseHourRepository;
    
        public CourseHoursService(ILearnSourceRepository learnSourceRepository, IClassRepository classRepository, ICourseHourRepository courseHourRepository)
        {
            _learnSourceRepository = learnSourceRepository;
            _classRepository = classRepository;
            _courseHourRepository = courseHourRepository;
        }
        public async Task AddAsync(CourseHour courseHour)
        {
            await _courseHourRepository.AddAsync(courseHour);
        }

        public async Task DeleteAsync(Guid id)
        {
            var courseHour = await _courseHourRepository.FindAsync(id);
            if (courseHour != null)
            {
                _courseHourRepository.Delete(courseHour);
            }
        }

        public async Task<List<CourseHour>?> GetByClassIdAndExpiredAsync(Guid classId)
        {
            var courseHour = await _courseHourRepository.GetByClassIdAsync(classId);
            return courseHour.Where(c => ClassRoomTimeTools.IsExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByClassIdAndNotExpiredAsync(Guid classId)
        {
            var courseHour = await _courseHourRepository.GetByClassIdAsync(classId);
            return courseHour.Where(c => ClassRoomTimeTools.NotExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByClassIdAndNotIngAsync(Guid classId)
        {
            var courseHour = await _courseHourRepository.GetByClassIdAsync(classId);
            return courseHour.Where(c => ClassRoomTimeTools.InClass(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByClassIdAsync(Guid classId)
        {
            return await _courseHourRepository.GetByClassIdAsync(classId);
        }

        public async Task<CourseHour> GetByIdAsync(Guid courseHourId)
        {
            return await _courseHourRepository.FindAsync(courseHourId);
        }

        public async Task<List<CourseHour>?> GetByStuIdAndExpiredAsync(string stuId)
        {
            var courseHour = await _courseHourRepository.GetByStuIdAsync(stuId);
            return courseHour.Where(c => ClassRoomTimeTools.IsExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByStuIdAndIngAsync(string stuId)
        {
            var courseHour = await _courseHourRepository.GetByStuIdAsync(stuId);
            return courseHour.Where(c => ClassRoomTimeTools.InClass(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByStuIdAndNotExpiredAsync(string stuId)
        {
            var courseHour = await _courseHourRepository.GetByStuIdAsync(stuId);
            return courseHour.Where(c => ClassRoomTimeTools.NotExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByStuIdAsync(string stuId)
        {
            return await _courseHourRepository.GetByStuIdAsync(stuId);
        }

        public async Task<List<CourseHour>?> GetByTeaIdAndExpiredAsync(string teaId)
        {
            var courseHour = await _courseHourRepository.GetByTeaIdAsync(teaId);
            return courseHour.Where(c => ClassRoomTimeTools.IsExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByTeaIdAndIngAsync(string teaId)
        {
            var courseHour = await _courseHourRepository.GetByTeaIdAsync(teaId);
            return courseHour.Where(c => ClassRoomTimeTools.InClass(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByTeaIdAndNotExpiredAsync(string teaId)
        {
            var courseHour = await _courseHourRepository.GetByTeaIdAsync(teaId);
            return courseHour.Where(c => ClassRoomTimeTools.NotExpired(c.ClassTime)).ToList();
        }

        public async Task<List<CourseHour>?> GetByTeaIdAsync(string teaId)
        {
            return await _courseHourRepository.GetByTeaIdAsync(teaId);
        }

        public async Task<bool> SaveAsync()
        {
            return await _courseHourRepository.SaveAsync();
        }

        public void UpdateAsync(CourseHour courseHour)
        {
            _courseHourRepository.Update(courseHour);
        }
    }
}